package com.linjian.togetherbirthday.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.linjian.togetherbirthday.utils.ViewFindUtils;
import com.linjian.togetherbirthday.view.custab.SlidingTabLayout;

import java.util.ArrayList;

public class MyOnADateFragment extends BaseFragment implements View.OnClickListener {

    private ImageView backIv;
    private TextView titleTv,rightTv;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"我发起的", "感兴趣的", "我报名的", "我参与的"};
    private MyPagerAdapter mAdapter;

    public static MyOnADateFragment newInstance(){
        MyOnADateFragment myOnADateFragment = new MyOnADateFragment();
        Bundle bundle = new Bundle();
        myOnADateFragment.setArguments(bundle);
        return myOnADateFragment;
    }

    @Override
    protected int setView() {
        return R.layout.fargment_my_onadate;
    }

    @Override
    protected void init(View view) {
        backIv = view.findViewById(R.id.layout_head_back_iv);
        titleTv = view.findViewById(R.id.layout_head_title_tv);
        rightTv = view.findViewById(R.id.layout_head_right_tv);

        mFragments.add(new MyOnADateOneFragment());
        mFragments.add(new MyOnADateTwoFragment());
        mFragments.add(new MyOnADateThreeFragment());
        mFragments.add(new MyOnADateFourFragment());
        View decorView = getActivity().getWindow().getDecorView();
        ViewPager vp = ViewFindUtils.find(decorView, R.id.vp);
        mAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
        vp.setAdapter(mAdapter);
        /** 默认 */
        SlidingTabLayout tabLayout_1 = ViewFindUtils.find(decorView, R.id.tl_1);
        tabLayout_1.setViewPager(vp);
        vp.setCurrentItem(0);
        tabLayout_1.showDot(0);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        titleTv.setText("我的约会");
        rightTv.setVisibility(View.GONE);
        setListener();
    }
    private void setListener() {
        backIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_head_back_iv:
                getActivity().getSupportFragmentManager().popBackStack();
                break;
        }
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
